﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Codeplex.Reactive;
using System.Reactive.Linq;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;

namespace HelloWorld.GreetModule
{
    public class GreetViewModel
    {
        [Import]
        public GreetModel Model { get; set; }

        [Required(ErrorMessage = "Error")]
        public ReactiveProperty<string> InputText { get; private set; }

        public ReactiveCommand GreetCommand { get; private set; }

        public GreetViewModel()
        {
            this.InputText = new ReactiveProperty<string>()
                .SetValidateAttribute(() => InputText);
            this.InputText.Value = string.Empty;

            this.InputText
                .Skip(1)
                .Subscribe(s =>
                {
                    this.Model.Message = s;
                });

            this.GreetCommand =
                this.InputText
                    .Select(_ => this.InputText.ForceValidate())
                    .Do(v => Debug.WriteLine("GreetCommand: " + v))
                    .Select(s => string.IsNullOrWhiteSpace(s))
                    .Do(v => Debug.WriteLine("GreetCommand: " + v))
                    .ToReactiveCommand(true);
            this.GreetCommand.Subscribe(_ => this.Model.Greet());
        }
    }
}
